import pygame
from didiball.planes.Goalpost import Goalpost
from didiball.planes.Shifting import ShiftingPlane

class Pitch(ShiftingPlane):
    GREEN=(58,154,59)
    GREEND=(60,150,60)
    COLKEY=(0,0,7)
    LINES_COLOUR=(255,255,255)

    def __init__(self,rect,scoreboard,other_dimensions):
        ShiftingPlane.__init__(self,(rect.left,rect.top))
        self.__rect= rect
        self.__other_dimensions=other_dimensions
        self.shiftables=[]
        self.make_halves() 
        self.make_goals(other_dimensions['goal'])
        self.make_areas(other_dimensions['area'])

        self.__scoreboard= scoreboard
        #self.init_zones()
        self.__surface= pygame.Surface((rect.width,rect.height))
        self.__surface.fill(self.COLKEY)
        self.__surface.set_colorkey(self.COLKEY)

    def make_goals(self,g):
        goals=[]
        p= self.__rect
        goals.append(pygame.Rect(p.left-int(g['depth']), p.top+p.height/2-int(g['breadth'])/2,int(g['depth']),int(g['breadth'])))
        goals.append(pygame.Rect(p.left+p.width, p.top+p.height/2-int(g['breadth'])/2,int(g['depth']),int(g['breadth'])))
        self.__goals= goals
        self.make_posts(goals)

    def make_halves(self):
        p= self.__rect
        self.__halves= []
        self.__halves.append(pygame.Rect(p.left,p.top,p.width/2,p.height))
        self.__halves.append(pygame.Rect(p.left+(p.width/2),p.top,p.width/2,p.height))

    def make_areas(self,a):
        areas=[]
        p= self.__rect
        areas.append(pygame.Rect(0, (p.height-a[1])/2,a[0],a[1]))
        areas.append(pygame.Rect((p.width-a[0]), (p.height-a[1])/2,a[0],a[1]))
        self.__areas= areas

    def make_post_surface(self):
        gp_surface= pygame.Surface((5,5))
        gp_surface.fill((255,255,255))
        #gp_surface= pygame.image.load(DISC_PATH).convert_alpha()
        return gp_surface

    def get_rect(self):
        return self.__rect

    def get_scoreboard(self):
        return self.__scoreboard
           
    def get_goals(self):
        return self.__goals

    def add_shiftables(self,shiftables):
        for p in shiftables:
            self.shiftables.append(p)

    def make_posts(self,goals):
        #self.__goals= goals
        self.add_shiftables(self.__goals)
        self.__posts = []
        self.__posts.append(Goalpost((goals[0].top,goals[0].right), (-1,-1), self.make_post_surface()))
        self.__posts.append(Goalpost((goals[0].top+goals[0].height,goals[0].right), (-1,1), self.make_post_surface()))
        self.__posts.append(Goalpost((goals[1].top,goals[1].left), (1,-1), self.make_post_surface()))
        self.__posts.append(Goalpost((goals[1].top+goals[1].height,goals[1].left), (1,1), self.make_post_surface()))
        self.add_shiftables(self.__posts)

    def grow(self,rect,h,w=0):
        return pygame.Rect(rect.left-w,rect.top-h,rect.width+(w*2),rect.height+(h*2))

    def get_surface(self):
        return self.__surface

    def draw_lines(self,screen,colour):
        for g in self.__goals:
            pygame.draw.rect(screen,colour,self.adjust_rect(g),3)
            #for z in self.__zones:
            #        pygame.draw.rect(screen,colour,self.adjust_rect(z),1)
        for a in self.__areas:
            pygame.draw.rect(screen,colour,self.adjust_rect(a),1)
        for half in self.__halves:
            pygame.draw.rect(screen,colour,self.adjust_rect(half),1)
                
        pygame.draw.circle(screen, colour, self.adjust_rect(self.get_rect()).center, self.__other_dimensions['circle'], 1)

    def undraw_lines(self,screen):
            self.draw_lines(screen,self.GREEN)
    def redraw_lines(self,screen):
            self.draw_lines(screen,self.LINES_COLOUR)

    def draw(self,screen):
        #surface = self.__surface
        #pr= pygame.draw.rect(surface,self.LINES_COLOUR,self.grow(self.get_rect(),2,2),3)
        pygame.draw.rect(screen,self.GREEN,self.get_rect())
                #gr= pygame.draw.rect(surface,self.GREY,g)
        self.redraw_lines(screen)

    def get_posts(self):
        return self.__posts

    def calc_basepoint(self, team_r2l, xpos, yidx, ycount):
        #print "Team: {0}, xpos: {1}, yIndex: {2}, ycount: {3}".format(team_id,xpos,yidx,ycount)

        if team_r2l==True:
                x=self.get_rect().left + (xpos*self.get_rect().width)
        else:
                x=self.get_rect().left + self.get_rect().width - (xpos*self.get_rect().width)

        yoff= (float(yidx+1)/float(ycount+1)*float(self.get_rect().height))
        y=self.get_rect().top + yoff 
        #print "X/Y: {0},{1} - h: {2},w: {3}, yoff: {4}".format(x,y,self.get_rect().height,self.get_rect().width, yoff)
        return x,y

    def calc_halfpoint(self, team_r2l, xpos, yidx, ycount):
        return self.calc_basepoint(team_r2l,xpos/2,yidx,ycount)

    def centre(self, coords):
        left= coords[0]-self.get_rect().width/2
        top= coords[1]-self.get_rect().height/2
        self.set_offset((left,top))
        #self.get_rect().top= coords[1]-self.get_rect().height/2
